//
//  SelectImproveView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/18.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol SelectImproveViewDelegate {
    
    @objc optional func selectBank(url:String,title:String) //选择银行
}

class SelectImproveView: UIView {
    
    weak var improveDelegate : SelectImproveViewDelegate?
    
    fileprivate let cellId = "selectImproveCell"
    
    fileprivate var tableView : UITableView!
    
    var dataArray = [[String:String]]() {
        didSet{
            tableView.reloadData()
            
        }
    }

    init() {
        super.init(frame: .zero)
    
        self.isHidden = true
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = UIColor.clear
        tableView.bounces = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SelectImproveCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(ScreenH/2)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SelectImproveView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 40)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? SelectImproveCell
        
//        if dataArray[indexPath.row]["img_src"]!.contains("http://www.kameng98.com") {
//            cell?.imageUrl = dataArray[indexPath.row]["img_src"]!
//        }else{
//            cell?.imageUrl = kmHttps + dataArray[indexPath.row]["img_src"]!
//        }
        
        cell?.bankText = dataArray[indexPath.row]["bank_text"]! + creditCard + liftingCheats
        cell?.bankImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: dataArray[indexPath.row]["bank_text"]!))
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        self.improveDelegate?.selectBank!(url: kmHttps + dataArray[indexPath.row]["bank_href"]!, title: dataArray[indexPath.row]["bank_text"]! + creditCard + liftingCheats)
    }
    
}
